package com.chen.framework.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.root.enums.RoleTypeEnums;
import com.chen.framework.model.auth.AuthRole;
import com.chen.framework.mapper.AuthRoleMapper;
import com.chen.framework.service.inter.AuthLinkRolePermissionService;
import com.chen.framework.service.inter.AuthRoleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: ~龙~
 * @date: 2023/6/13 17:15
 **/
@Service
@AllArgsConstructor
public class AuthRoleServiceImpl extends ServiceImpl<AuthRoleMapper, AuthRole> implements AuthRoleService {

    private final AuthLinkRolePermissionService authLinkRolePermissionService;

    @Override
    public void superAdminEmpowerAll(Set<Long> permissionIdSet, RoleTypeEnums enums) {
        if (CollectionUtil.isEmpty(permissionIdSet)){
            return;
        }

        List<Long> roleIdList = list(
                Wrappers.lambdaQuery(AuthRole.class)
                        .select(AuthRole::getRoleId)
                        .eq(AuthRole::getType, enums.getValue())
        ).stream().map(AuthRole::getRoleId).collect(Collectors.toList());

        authLinkRolePermissionService.roleAndPermissionAssociation(roleIdList,permissionIdSet);
    }
}
